/* include/modules/cmos.h 
 * 
 * Copyright (C) 2021, 2022 intirain. 
 *  
 * This file is part of evx. 
 * 
 * evx is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * evx is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with evx. If not, see <https://www.gnu.org/licenses/>
 */ 




#ifndef __MOD_CMOS_H
#define __MOD_CMOS_H

#ifndef __ASSEMBLER__

#include <arch/io.h>
#include <kernel/time.h>

#define CMOS_READ(addr) ({	\
	outb(addr, 0x70);	\
	inb(0x71); })

/* For time and they actually come from linux.  */
#define BCD2BIN(byte) ((byte) = ((byte) & 0xf) + ((byte) >> 4) * 10)
static inline void read_hw_clock(struct ktime *tm) {
	do {
		tm->sec = CMOS_READ(0);
		tm->min = CMOS_READ(2);
		tm->hour = CMOS_READ(4);
		tm->day = CMOS_READ(7);
		tm->month = CMOS_READ(8) - 1;
		tm->year = CMOS_READ(9);
	} while (tm->sec != CMOS_READ(0));

	BCD2BIN(tm->sec); BCD2BIN(tm->min); BCD2BIN(tm->hour);
	BCD2BIN(tm->day); BCD2BIN(tm->month); BCD2BIN(tm->year);

	tm->year += tm->year >= 70 ? 1900 : 2000;
	tm->month++;
}

#endif
#endif

